import java.util.Locale;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ayuku.commons.SqliteDB;
import com.ayuku.course.report.EmployeeReport;
import com.ayuku.course.ui.AboutDlg;
import com.ayuku.course.ui.coursecategory.ListCategory;
import com.ayuku.course.ui.courselevel.ListCourseLevel;
import com.ayuku.course.ui.coursepackage.ListCoursePackage;
import com.ayuku.course.ui.employee.ListPegawai;

/**
 * 
 * @author putukus
 * 
 */
public class MainWindowCourse extends Shell {

	public static CTabFolder tabFolderMain;
	int i = 0;
	private CTabItem tabListMain;
	private Label lblBreadCrumb;
	private Label lblStatus1;
	private Label lblStatus2;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		Locale.setDefault(Locale.US);
		try {
			// AyukuDB.openDB();
			SqliteDB.openDB();
			Display display = Display.getDefault();
			MainWindowCourse shell = new MainWindowCourse(display);
			shell.setMaximized(true);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public MainWindowCourse(Display display) {

		super(display, SWT.SHELL_TRIM);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent arg0) {
				SWTResourceManager.dispose();
				System.out.println("App Disposed......");
			}
		});
		setMinimumSize(new Point(800, 550));
		setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		setSize(685, 464);

		System.out.println("Makan nasi...");

		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginRight = 5;
		setLayout(gridLayout);

		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		toolBar.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND));
		toolBar
				.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				openAboutDialog();
			}
		});
		tltmNewItem_2.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/user_suit.png"));
		tltmNewItem_2.setText("About");

		ToolItem tltmNewItem_3 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
			}
		});
		tltmNewItem_3.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/help.png"));
		tltmNewItem_3.setText("Help");

		Label label = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

		lblBreadCrumb = new Label(this, SWT.NONE);
		lblBreadCrumb.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		lblBreadCrumb.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,
				1, 1));
		lblBreadCrumb.setText("Report > Employee");

		SashForm sashForm = new SashForm(this, SWT.NONE);
		sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		ExpandBar expandBar = new ExpandBar(sashForm, SWT.V_SCROLL);
		expandBar.setBackgroundMode(SWT.INHERIT_DEFAULT);
		expandBar.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));

		ExpandItem expandMenuMaster = new ExpandItem(expandBar, SWT.NONE);
		expandMenuMaster.setExpanded(true);
		expandMenuMaster.setImage(SWTResourceManager.getImage(
				MainWindowCourse.class, "/images/form/database_add.png"));
		expandMenuMaster.setText("Master Data");

		Composite cmpMenuMaster = new Composite(expandBar, SWT.NONE);
		expandMenuMaster.setControl(cmpMenuMaster);
		expandMenuMaster.setHeight(110);
		GridLayout gl_cmpMenuMaster = new GridLayout(1, false);
		gl_cmpMenuMaster.marginRight = 5;
		gl_cmpMenuMaster.marginLeft = 5;
		gl_cmpMenuMaster.verticalSpacing = 4;
		gl_cmpMenuMaster.marginWidth = 1;
		gl_cmpMenuMaster.marginHeight = 4;
		cmpMenuMaster.setLayout(gl_cmpMenuMaster);

		CLabel lblNewLabel_7 = new CLabel(cmpMenuMaster, SWT.NONE);
		lblNewLabel_7.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/application_form.png"));
		lblNewLabel_7.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent arg0) {
				linkCategoryClicked(arg0);
			}
		});

		lblNewLabel_7.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		lblNewLabel_7.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		lblNewLabel_7.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		lblNewLabel_7.setText("Category");

		CLabel lblNewLabel_8 = new CLabel(cmpMenuMaster, SWT.NONE);
		lblNewLabel_8.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/application_form.png"));
		lblNewLabel_8.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent arg0) {
				showCourseLevel(arg0);
			}
		});
		lblNewLabel_8.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		lblNewLabel_8.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		lblNewLabel_8.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		lblNewLabel_8.setText("Course Level");

		CLabel lblNewLabel_9 = new CLabel(cmpMenuMaster, SWT.NONE);
		lblNewLabel_9.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/application_form.png"));
		lblNewLabel_9.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent arg0) {
				mnuCourseProductsClicked(arg0);
			}
		});
		lblNewLabel_9.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		lblNewLabel_9.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		lblNewLabel_9.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		lblNewLabel_9.setText("Course Package");

		CLabel lblNewLabel_10 = new CLabel(cmpMenuMaster, SWT.NONE);
		lblNewLabel_10.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/application_form.png"));
		lblNewLabel_10.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent arg0) {
				showEmployee(arg0);
			}
		});
		lblNewLabel_10.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		lblNewLabel_10.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		lblNewLabel_10.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		lblNewLabel_10.setText("Employee");

		Composite cmpMenuTrx = new Composite(expandBar, SWT.NONE);
		GridLayout gl_cmpMenuTrx = new GridLayout(1, false);
		gl_cmpMenuTrx.marginLeft = 5;
		gl_cmpMenuTrx.marginRight = 5;
		gl_cmpMenuTrx.marginWidth = 1;
		gl_cmpMenuTrx.marginHeight = 1;
		gl_cmpMenuTrx.horizontalSpacing = 1;
		cmpMenuTrx.setLayout(gl_cmpMenuTrx);

		CLabel linkSiswa = new CLabel(cmpMenuTrx, SWT.NONE);
		linkSiswa.setEnabled(false);
		linkSiswa.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));

		linkSiswa.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/user_suit.png"));
		linkSiswa.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		linkSiswa
				.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		linkSiswa.setText("Student");

		ExpandItem expandMenuLaporan = new ExpandItem(expandBar, SWT.NONE);
		expandMenuLaporan.setExpanded(true);
		expandMenuLaporan.setImage(SWTResourceManager.getImage(
				MainWindowCourse.class, "/images/form/page_white_stack.png"));
		expandMenuLaporan.setText("Reports");

		Composite cmpMenuLaporan = new Composite(expandBar, SWT.NONE);
		expandMenuLaporan.setControl(cmpMenuLaporan);
		expandMenuLaporan.setHeight(50);
		GridLayout gl_cmpMenuLaporan = new GridLayout(1, false);
		gl_cmpMenuLaporan.marginRight = 5;
		gl_cmpMenuLaporan.marginLeft = 5;
		gl_cmpMenuLaporan.marginWidth = 1;
		gl_cmpMenuLaporan.marginHeight = 1;
		cmpMenuLaporan.setLayout(gl_cmpMenuLaporan);

		CLabel menuLaporanPegawai = new CLabel(cmpMenuLaporan, SWT.NONE);
		menuLaporanPegawai.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent arg0) {
				menuLaporanPegawaiClicked(arg0);
			}
		});
		menuLaporanPegawai.setImage(SWTResourceManager.getImage(
				MainWindowCourse.class, "/images/form/page_white_text.png"));
		menuLaporanPegawai.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		menuLaporanPegawai.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		menuLaporanPegawai.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		menuLaporanPegawai.setText("Employee");

		Composite cmpMenuSetting = new Composite(expandBar, SWT.NONE);
		GridLayout gl_cmpMenuSetting = new GridLayout(1, false);
		gl_cmpMenuSetting.marginRight = 5;
		gl_cmpMenuSetting.marginLeft = 5;
		gl_cmpMenuSetting.marginHeight = 1;
		gl_cmpMenuSetting.marginWidth = 1;
		cmpMenuSetting.setLayout(gl_cmpMenuSetting);

		CLabel lblNewLabel_3 = new CLabel(cmpMenuSetting, SWT.NONE);
		lblNewLabel_3.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		lblNewLabel_3.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		lblNewLabel_3.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/database_delete.png"));
		lblNewLabel_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		lblNewLabel_3.setText("Setting");

		CLabel lblNewLabel_4 = new CLabel(cmpMenuSetting, SWT.NONE);
		lblNewLabel_4.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		lblNewLabel_4.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		lblNewLabel_4.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/database_save.png"));
		lblNewLabel_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		lblNewLabel_4.setText("Backup");

		CLabel lblNewLabel_5 = new CLabel(cmpMenuSetting, SWT.NONE);
		lblNewLabel_5.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		lblNewLabel_5.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		lblNewLabel_5.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/database_add.png"));
		lblNewLabel_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		lblNewLabel_5.setText("Restore");

		Composite mainComposite = new Composite(sashForm, SWT.NONE);
		mainComposite.setLayout(new FormLayout());

		tabFolderMain = new CTabFolder(mainComposite, SWT.BORDER);
		tabFolderMain.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		FormData fd_tabFolderMain = new FormData();
		fd_tabFolderMain.bottom = new FormAttachment(100, -64);
		fd_tabFolderMain.top = new FormAttachment(0);
		fd_tabFolderMain.left = new FormAttachment(0);
		fd_tabFolderMain.right = new FormAttachment(100);
		tabFolderMain.setLayoutData(fd_tabFolderMain);
		tabFolderMain.setSelectionBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		tabListMain = new CTabItem(tabFolderMain, SWT.NONE);
		tabListMain.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/database_add.png"));
		tabListMain.setText("List");

		TabFolder tabFolder = new TabFolder(mainComposite, SWT.NONE);
		FormData fd_tabFolder = new FormData();
		fd_tabFolder.top = new FormAttachment(100, -58);
		fd_tabFolder.bottom = new FormAttachment(100, 4);
		fd_tabFolder.right = new FormAttachment(100);
		fd_tabFolder.left = new FormAttachment(0);
		tabFolder.setLayoutData(fd_tabFolder);

		TabItem tbtmTips = new TabItem(tabFolder, SWT.NONE);
		tbtmTips.setText("Tips");

		StyledText styledText = new StyledText(tabFolder, SWT.BORDER);
		styledText.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
		tbtmTips.setControl(styledText);
		styledText
				.setText("   Please use Add  button for Add new Row,  Edit button for modify, Delete for delete row\r\n");

		TabItem tbtmNewItem = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem.setText("Another Tab");

		TabItem tbtmAnotherTab = new TabItem(tabFolder, SWT.NONE);
		tbtmAnotherTab.setText("Another Tab");
		sashForm.setWeights(new int[]{1, 4});

		Menu menu = new Menu(this, SWT.BAR);
		setMenuBar(menu);

		MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
		mntmFile.setText("File");

		Menu menu_1 = new Menu(mntmFile);
		mntmFile.setMenu(menu_1);

		MenuItem mntmExit = new MenuItem(menu_1, SWT.NONE);
		mntmExit.setText("Exit");

		Composite composite_9 = new Composite(this, SWT.NONE);
		composite_9.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		composite_9.setLayout(new GridLayout(2, false));
		GridData gd_composite_9 = new GridData(SWT.FILL, SWT.FILL, false, false, 1,
				1);
		gd_composite_9.heightHint = 29;
		composite_9.setLayoutData(gd_composite_9);

		lblStatus1 = new Label(composite_9, SWT.NONE);
		lblStatus1
				.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

		lblStatus2 = new Label(composite_9, SWT.NONE);
		lblStatus2
				.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		createContents();
	}

	protected void openAboutDialog() {
		AboutDlg ab = new AboutDlg(getShell(), SWT.DIALOG_TRIM);
		 ab.open();
		
	}

	protected void menuLaporanPegawaiClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		EmployeeReport rpt = new EmployeeReport(this, SWT.NONE);
		rpt.open();
	}

	protected void linkCategoryClicked(MouseEvent arg0) {
		ListCategory lst = new ListCategory(tabFolderMain, SWT.NONE);
		tabListMain.setControl(lst);
		tabListMain.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/database_add.png"));
		tabListMain.setText("Course Category  ");
		tabFolderMain.setSelection(0);
		lblBreadCrumb.setText("Master Data > Course Category");
	}

	protected void mnuCourseProductsClicked(MouseEvent arg0) {
		ListCoursePackage lst = new ListCoursePackage(tabFolderMain, SWT.NONE);
		tabListMain.setControl(lst);
		tabListMain.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/database_add.png"));
		tabListMain.setText("Course Package  ");
		tabFolderMain.setSelection(0);
		lblBreadCrumb.setText("Master Data > Course Package");
	}

	protected void showCourseLevel(MouseEvent arg0) {
		ListCourseLevel lst = new ListCourseLevel(tabFolderMain, SWT.NONE);
		tabListMain.setControl(lst);
		tabListMain.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/database_add.png"));
		tabListMain.setText("Course Level ");
		tabFolderMain.setSelection(0);
		lblBreadCrumb.setText("Master Data > Cuourse Level");
	}

	protected void showEmployee(MouseEvent arg0) {
		ListPegawai lst = new ListPegawai(tabFolderMain, SWT.NONE);
		tabListMain.setControl(lst);
		tabListMain.setImage(SWTResourceManager.getImage(MainWindowCourse.class,
				"/images/form/database_add.png"));
		tabListMain.setText("Employee List  ");
		tabFolderMain.setSelection(0);
		lblBreadCrumb.setText("Master Data> Employee");
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Simple Course App");
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
